package com.lin.gameserver.util;

/**
 * 十六进制工具类，用于将二进制数据转换为十六进制字符串，方便调试和日志记录
 */
public class HexUtil {
    private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 将二进制数据转换为十六进制字符串
     *
     * @param data 要转换的二进制数据
     * @return 十六进制字符串
     */
    public static String toHexString(byte[] data) {
        if (data == null) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (byte b : data) {
            sb.append(HEX_CHARS[(b & 0xF0) >>> 4]);
            sb.append(HEX_CHARS[b & 0x0F]);
        }
        return sb.toString();
    }

    /**
     * 将二进制数据转换为十六进制字符串，并限制长度
     *
     * @param data 要转换的二进制数据
     * @param maxBytes 最大转换的字节数
     * @return 十六进制字符串
     */
    public static String toHexString(byte[] data, int maxBytes) {
        if (data == null) {
            return "";
        }
        
        int length = Math.min(data.length, maxBytes);
        StringBuilder sb = new StringBuilder(length * 2);
        for (int i = 0; i < length; i++) {
            byte b = data[i];
            sb.append(HEX_CHARS[(b & 0xF0) >>> 4]);
            sb.append(HEX_CHARS[b & 0x0F]);
        }
        
        if (data.length > maxBytes) {
            sb.append("...(").append(data.length - maxBytes).append(" more)");
        }
        
        return sb.toString();
    }

    /**
     * 将二进制数据转换为十六进制字符串，添加分隔符，便于阅读
     *
     * @param data 要转换的二进制数据
     * @param separator 分隔符
     * @return 带分隔符的十六进制字符串
     */
    public static String toHexStringWithSeparator(byte[] data, String separator) {
        if (data == null) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder(data.length * 3);
        for (int i = 0; i < data.length; i++) {
            byte b = data[i];
            sb.append(HEX_CHARS[(b & 0xF0) >>> 4]);
            sb.append(HEX_CHARS[b & 0x0F]);
            
            if (i < data.length - 1) {
                sb.append(separator);
            }
        }
        
        return sb.toString();
    }

    /**
     * 将十六进制字符串转换为二进制数据
     *
     * @param hexString 十六进制字符串
     * @return 二进制数据
     */
    public static byte[] fromHexString(String hexString) {
        if (hexString == null || hexString.length() == 0) {
            return new byte[0];
        }
        
        // 确保字符串长度为偶数
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }
        
        int length = hexString.length() / 2;
        byte[] result = new byte[length];
        
        for (int i = 0; i < length; i++) {
            int high = Character.digit(hexString.charAt(i * 2), 16);
            int low = Character.digit(hexString.charAt(i * 2 + 1), 16);
            
            if (high == -1 || low == -1) {
                throw new IllegalArgumentException("Invalid hex string: " + hexString);
            }
            
            result[i] = (byte) ((high << 4) | low);
        }
        
        return result;
    }
} 